//引axios
import axios from "axios";

//引入进度条
import nProgress from "nprogress";
import "nprogress/nprogress.css";


//创建axios实例
const request = axios.create({
    baseURL: process.env.VUE_APP_MOCKAPI,
    timeout: 10000,
})

//给request实例配置拦截器
request.interceptors.request.use(
    //发送请求成功
    (config) => {
        //开启进度条
        nProgress.start()

        return config
    },
    //发送请求失败
    (error) => {
        //关闭进度条
        nProgress.done()

        return Promise.reject(error)
    }
)

request.interceptors.response.use(
    //响应成功
    (response) => {
        //关闭进度条
        nProgress.done()

        //响应成功把真实的数据返回出去
        if(response.data.code === 200){
            return response.data.data
        }else{
            //自己封装一个失败对象
            return Promise.reject({meaaage: response.data.message})
        }
    },

    //响应失败
    (error) => {
        //关闭进度条
        nProgress.done()

        return Promise.reject(error)
    }
)

//暴露
export default request